package com.uchedao.seller.util;

import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.nostra13.universalimageloader.utils.L;
import com.uchedao.seller.R;
import com.uchedao.seller.config.GlobalInfo;
import com.uchedao.seller.http.entity.DictEntity;
import com.uchedao.seller.inf.ICheckEntityListener;
import com.uchedao.seller.inf.ICheckValueListener;
import com.uchedao.seller.widget.dialog_for_ios.ZAlertDialog;

import java.util.List;


public class CheckTextBtnValue {

	private ZAlertDialog.Builder builder;
	private TextView tv;
	private Button btn;
	private String[] array;
	private List<DictEntity> list;
	private ICheckValueListener listener;
	private ICheckEntityListener entityListener;

	public CheckTextBtnValue(ViewGroup viewGroup, int tvResId, int btnResId, int arrayResId, String text,
			ICheckValueListener listener) {
		builder = new ZAlertDialog.Builder(viewGroup.getContext());
		array = GlobalInfo.resources.getStringArray(arrayResId);
		this.listener = listener;
		tv = (TextView) viewGroup.findViewById(tvResId);
		btn = (Button) viewGroup.findViewById(btnResId);
		btn.addTextChangedListener(textStarWatcher);
		btn.setText(text);
		btn.setOnClickListener(buttonOnClickListener);
//		if (!TextUtils.isEmpty(text) && listener != null)
//			listener.getValue(btn, text);
	}

	public CheckTextBtnValue(ViewGroup viewGroup, int tvResId, int btnResId, List<DictEntity> list, String text,
							 ICheckEntityListener entityListener) {
		builder = new ZAlertDialog.Builder(viewGroup.getContext());
		this.list = list;
		this.entityListener = entityListener;
		tv = (TextView) viewGroup.findViewById(tvResId);
		btn = (Button) viewGroup.findViewById(btnResId);
		btn.addTextChangedListener(textStarWatcher);
		btn.setText(text);
		btn.setOnClickListener(buttonEntityListener);
//		if (!TextUtils.isEmpty(text) && listener != null)
//			listener.getValue(btn, text);
	}

	/**
	 * 只监听判断是否显示星号，不做其他处理
	 *
	 * @param viewGroup
	 * @param tvResId
	 * @param btnResId
	 */
	public CheckTextBtnValue(ViewGroup viewGroup, int tvResId, int btnResId, String text) {
		builder = new ZAlertDialog.Builder(viewGroup.getContext());
		tv = (TextView) viewGroup.findViewById(tvResId);
		btn = (Button) viewGroup.findViewById(btnResId);
		btn.setText("");
		btn.addTextChangedListener(textStarWatcher);
		if (!TextUtils.isEmpty(text)&&!text.contains("null")){
			btn.setText(text);
		}else{
			btn.setText("");
		}


	}

	/**
	 * 监听有值时显示星号
	 *
	 * @author "叶晋"
	 *
	 */
	TextWatcher textStarWatcher = new TextWatcher() {

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			L.i(tv + " " + s.toString());
			if (tv != null)
				setStar(tv, TextUtils.isEmpty(s));


			if (listener != null)
				listener.getValue(tv, s.toString());
		}

		/**
		 * 设置是否显示星号
		 *
		 * @param tv
		 * @param show
		 */
		void setStar(TextView tv, boolean show) {
			tv.setCompoundDrawablesWithIntrinsicBounds(show ? R.mipmap.read_star : R.mipmap.read_star_no, 0, 0, 0);
		}
	};

	/**
	 * 按钮按下时
	 *
	 * @author "叶晋"
	 *
	 */
	OnClickListener buttonOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			builder.setTitle(tv.getText()).setItems(array, listDialogOnClickListener).show();
		}
	};

	/**
	 * 监听对话框列表的选值
	 *
	 * @author "叶晋"
	 *
	 */
	DialogInterface.OnClickListener listDialogOnClickListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			btn.setText(array[which]);
			if (listener != null)
				listener.getValue(btn, array[which]);
		}

	};

	OnClickListener buttonEntityListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(list == null)
				return;

			array = new String[list.size()];
			for(int i=0; i<list.size(); i++)
				array[i] = list.get(i).caption;

			builder.setTitle(tv.getText()).setItems(array, listDialogEntityListener).show();
		}
	};

	DialogInterface.OnClickListener listDialogEntityListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			btn.setText(array[which]);
			if (entityListener != null)
				entityListener.getValue(btn, list.get(which));
		}

	};
}
